package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.*;
import com.my.service.MotherNursingService;
import com.my.service.VarietyService;
import com.my.util.SessionUtil;
import com.sun.media.sound.JARSoundbankReader;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author 闫亚茹
 * @since 2018-08-29
 */
@Controller
@RequestMapping("/variety")
public class VarietyController {
    Logger logger=Logger.getLogger(WorkguidaceController.class);
    @Autowired
    VarietyService varietyService;

        //添加
        @RequestMapping("/addvariety")
        @ResponseBody
        public String addvariety( String  param,HttpSession session) {
            Variety variety =JSONObject.parseObject(param,Variety.class);
            variety.setCreatetime(new Date().getTime());
            Long areaid=((User)session.getAttribute("user")).getAreaId();
            variety.setAreaid(areaid);
            if (varietyService.insert(variety)) {
                logger.info("{addvariety|保存成功}");
                return "保存成功";
            } else {
                logger.info("{addvariety|保存失败}");
                return "保存失败";
            }
        }

        //查询
        @RequestMapping("/selectVarietyPaGe")
        @ResponseBody
        public ResultPage<Variety> selectVarietyPage(String lin, String data, VoPage voPage, HttpSession session){
            boolean admin=false;
            //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
            if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
                admin=true;
            }
            Map map=new HashMap();
            map.put("lin",lin);
            map.put("data",data);
            Long areaid=((User)session.getAttribute("user")).getAreaId();
            ResultPage<Variety> resultPage = varietyService.selectMyPage(map,voPage,areaid,admin);
            return  resultPage;

        }
        //查询所有菜品
        @RequestMapping("/selectAllVariety")
        @ResponseBody
        public List<Variety> selectAllVariety(){
            Map map=new HashMap();
            List<Variety> resultPage = varietyService.selectByMap(map);
            return  resultPage;

        }


    /**
     * 跳转页面
     * @return
     */
        @RequestMapping("/variety")
        public  String  variety(){
            return "Adddishes";
        }

    /**
     * 删除菜品
     * @param id
     * @return
     */
    @RequestMapping("/deleteVariety")
    @ResponseBody
    public boolean deleteVariety(Long id){
            return  varietyService.deleteById(id);
    }


    /**
     * 修改菜品
     * @param variety
     * @return
     */
    @RequestMapping("/updateVariety")
    @ResponseBody
    public String updateVariety(Variety variety){
        List<Variety> list = new ArrayList<>();
        list.add(variety);
        if(varietyService.updateBatchById(list)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }
    @RequestMapping("/Adddishes_Add")
    public  String  Adddishes_Add(){
        return "/Alert/Adddishes_Add";
    }



    }


